package com.jia.sms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.sms.dto.LoginFromDto;
import com.jia.sms.entity.User;
import com.jia.sms.holder.UserHolder;
import com.jia.sms.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2024-09-28 16:33:43
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param page 分页对象
     * http://localhost:8080/api/user/page?current=2&size=2
     * @return 查询结果
     */
    @GetMapping("/page")
    public ResponseEntity<Page<User>> queryByPage(User user, Page page) {
        return ResponseEntity.ok(this.userService.queryByPage(user, page));
    }

    /**
     * 发送验证码
     *
     * @param phone   手机号
     * @param session session
     * @return 实例对象
     */
    @PostMapping("code")
    public ResponseEntity<String> sendCode(@RequestParam("phone") String phone, HttpSession session) {
        userService.sendCode(phone, session);
        return ResponseEntity.ok("success");
    }

    /**
     * 验证码登录
     *
     * @param loginFromDto 登录实体
     * @param session      session
     * @return 实例对象
     */
    @PostMapping("login")
    public ResponseEntity<String> login(@RequestBody LoginFromDto loginFromDto, HttpSession session) {
        String str = userService.login(loginFromDto, session);
        return ResponseEntity.ok(str);
    }

    /**
     * 登出
     *
     * @return 实例对象
     */
    @PostMapping("logout")
    public ResponseEntity<String> logout() {
        return ResponseEntity.ok("功能未完成");
    }


    /**
     * 获取当前用户
     *
     * @return 用户信息
     */
    @GetMapping("me")
    public ResponseEntity<User> me() {
        return ResponseEntity.ok(UserHolder.getUser());
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<User> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<User> add(@RequestBody User user) {
        return ResponseEntity.ok(this.userService.insert(user));
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<User> edit(@RequestBody User user) {
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.userService.deleteById(id));
    }

}

